package Lianxi.ATM;

import Lianxi.Student.Stundent;
import MySql.Sql;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Presmt {
    Sql sql = new Sql();

    public void insert(Stundent stundent) {

        Connection con = sql.getConnection();
        java.sql.PreparedStatement statement = null;
        String mysql = "insert into t_atm(atm_id,atm_pwd,atm_money)values(?,?,?)";
        try {
            statement = con.prepareStatement(mysql);
            statement.setString(1, stundent.getId());
            statement.setString(2, stundent.getGetPassword());
            statement.setInt(3, stundent.getMoney());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            guanbi(con, statement);
        }
    }

    public List chaXun(String name) {
        Connection con = sql.getConnection();
        java.sql.PreparedStatement statement=null;
        ResultSet rs = null;
        List list = new ArrayList();
        String sql = "select * from t_atm where atm_id=?";
        try {
            statement=con.prepareStatement(sql);
            statement.setObject(1,name);
            rs=statement.executeQuery();
            while (rs.next()){
                User user=new User();
                user.setId(rs.getString("atm_id"));
                user.setId(rs.getString("atm_pwd"));
                user.setId(rs.getString("atm_money"));
                list.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            guanbi(con, statement);
            if (rs!=null){
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
         return list;
    }

    private void guanbi(Connection con, java.sql.PreparedStatement statement) {
        if (con!=null){
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        Presmt presmt = new Presmt();
        User user=new User();
        user.setId("yang");
        presmt.chaXun("yang");


    }
}

